package com.x.uikit.toolbar;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.view.animation.AccelerateDecelerateInterpolator;

public class XTabToolbar extends XToolbar {
    protected String mSelectedToken;
    protected ValueAnimator mAnimator = ValueAnimator.ofFloat(0, 1);
    protected long mAnimateDuration = 300;
    protected TimeInterpolator mAnimateInterpolator = new AccelerateDecelerateInterpolator();

    public XTabToolbar(Context context) {
        super(context);
    }

    public void selectItem(String token) {
        selectItem(token, true);
    }

    public void setItemSelectAnimateDuration(long duration) {
        this.mAnimateDuration = duration;
    }

    public void setAnimateInterpolator(TimeInterpolator interpolator) {
        if (interpolator == null) {
            return;
        }
        this.mAnimateInterpolator = interpolator;
    }

    public void selectItem(String token, boolean animate) {
        String lastSelected = mSelectedToken;
        updateSelectItem(lastSelected, token, animate);
        this.mSelectedToken = token;
    }

    public void updateSelectItemProgress(String token, float progress) {
        XToolbarItem lastSelectItem = mItems.get(mSelectedToken);
        if (lastSelectItem != null) {
            lastSelectItem.setItemSelectedProgress(1 - progress);
        }
        XToolbarItem newSelectItem = mItems.get(token);
        if (newSelectItem != null) {
            newSelectItem.setItemSelectedProgress(progress);
        }
    }

    protected void updateSelectItem(String lastSelected, String newSelected, boolean animate) {
        if (TextUtils.equals(lastSelected, newSelected)) {
            return;
        }
        final XToolbarItem lastSelectItem;
        if (!TextUtils.isEmpty(lastSelected)) {
            lastSelectItem = mItems.get(lastSelected);
        } else {
            lastSelectItem = null;
        }
        final XToolbarItem newSelectItem;
        if (!TextUtils.isEmpty(newSelected)) {
            newSelectItem = mItems.get(newSelected);
        } else {
            newSelectItem = null;
        }
        if (lastSelectItem != null || newSelectItem != null) {
            if (animate && mAnimateDuration > 0) {
                mAnimator.cancel();
                mAnimator.removeAllUpdateListeners();
                mAnimator.removeAllListeners();
                mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float progress = (float) animation.getAnimatedValue();
                        if (lastSelectItem != null) {
                            lastSelectItem.setItemSelectedProgress(1 - progress);
                        }
                        if (newSelectItem != null) {
                            newSelectItem.setItemSelectedProgress(progress);
                        }
                    }
                });
                mAnimator.setDuration(mAnimateDuration);
                mAnimator.setInterpolator(mAnimateInterpolator);
                mAnimator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                    }
                });
                mAnimator.start();
            } else {
                if (lastSelectItem != null) {
                    lastSelectItem.setItemSelected(false);
                }
                if (newSelectItem != null) {
                    newSelectItem.setItemSelected(true);
                }
            }
        }
    }
}
